using Microsoft.Phone.Controls.Primitives;
using Microsoft.Phone.Gestures;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Microsoft.Phone.Controls
{
	[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(PivotItem)), TemplatePart(Name = "PivotItemPresenter", Type = typeof(ItemsPresenter)), TemplatePart(Name = "HeadersListElement", Type = typeof(PivotHeadersControl))]
	public class Pivot : TemplatedItemsControl<PivotItem>
	{
		private const string ElementHeadersRowDefinitionName = "HeadersRowDefinition";
		private const string HeadersListElement = "HeadersListElement";
		private const string PivotItemPresenterElement = "PivotItemPresenter";
		internal const string ItemContainerStyleName = "ItemContainerStyle";
		private const double pixelsPerSecondTemporary = 600.0;
		internal const double PivotAnimationSeconds = 0.25;
		private static readonly TimeSpan PivotAnimationTimeSpan = TimeSpan.FromSeconds(0.25);
		internal static readonly Duration PivotAnimationDuration = new Duration(Pivot.PivotAnimationTimeSpan);
		internal static readonly Duration ZeroDuration = new Duration(TimeSpan.Zero);
		internal readonly IEasingFunction QuarticEase = new QuarticEase();
		private PivotHeadersControl _headers;
		private ItemsPresenter _itemsPresenter;
		private Panel _itemsPanel;
		private AnimationDirection? _animationHint = default(AnimationDirection?);
		private bool _updatingHeaderItems;
		private bool _ignorePropertyChange;
		internal PivotHeadersControl _clickedHeadersControl;
		private bool _animating;
		private bool _isHorizontalDragging;
		private double _actualWidth;
		private bool _isDesignTime;
		private bool _skippedLoadingPivotItem;
		private bool _skippedSwapVisibleContent;
		private bool _templateApplied;
		private Queue<int> _queuedIndexChanges;
		private TransformAnimator _panAnimator;
		public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(Pivot), new PropertyMetadata(null));
		public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(Pivot), new PropertyMetadata(new PropertyChangedCallback(Pivot.OnSelectedIndexPropertyChanged)));
		public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(Pivot), new PropertyMetadata(null, new PropertyChangedCallback(Pivot.OnSelectedItemPropertyChanged)));
		public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(object), typeof(Pivot), new PropertyMetadata(null));
		public static readonly DependencyProperty TitleTemplateProperty = DependencyProperty.Register("TitleTemplate", typeof(DataTemplate), typeof(Pivot), new PropertyMetadata(null));
		public event EventHandler<PivotItemEventArgs> LoadingPivotItem
		{
			[MethodImpl(32)]
			add
			{
				this.LoadingPivotItem = (EventHandler<PivotItemEventArgs>)Delegate.Combine(this.LoadingPivotItem, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.LoadingPivotItem = (EventHandler<PivotItemEventArgs>)Delegate.Remove(this.LoadingPivotItem, value);
			}
		}
		public event EventHandler<PivotItemEventArgs> LoadedPivotItem
		{
			[MethodImpl(32)]
			add
			{
				this.LoadedPivotItem = (EventHandler<PivotItemEventArgs>)Delegate.Combine(this.LoadedPivotItem, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.LoadedPivotItem = (EventHandler<PivotItemEventArgs>)Delegate.Remove(this.LoadedPivotItem, value);
			}
		}
		public event EventHandler<PivotItemEventArgs> UnloadingPivotItem
		{
			[MethodImpl(32)]
			add
			{
				this.UnloadingPivotItem = (EventHandler<PivotItemEventArgs>)Delegate.Combine(this.UnloadingPivotItem, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.UnloadingPivotItem = (EventHandler<PivotItemEventArgs>)Delegate.Remove(this.UnloadingPivotItem, value);
			}
		}
		public event EventHandler<PivotItemEventArgs> UnloadedPivotItem
		{
			[MethodImpl(32)]
			add
			{
				this.UnloadedPivotItem = (EventHandler<PivotItemEventArgs>)Delegate.Combine(this.UnloadedPivotItem, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.UnloadedPivotItem = (EventHandler<PivotItemEventArgs>)Delegate.Remove(this.UnloadedPivotItem, value);
			}
		}
		public event SelectionChangedEventHandler SelectionChanged
		{
			[MethodImpl(32)]
			add
			{
				this.SelectionChanged = (SelectionChangedEventHandler)Delegate.Combine(this.SelectionChanged, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.SelectionChanged = (SelectionChangedEventHandler)Delegate.Remove(this.SelectionChanged, value);
			}
		}
		public DataTemplate HeaderTemplate
		{
			get
			{
				return base.GetValue(Pivot.HeaderTemplateProperty) as DataTemplate;
			}
			set
			{
				base.SetValue(Pivot.HeaderTemplateProperty, value);
			}
		}
		public int SelectedIndex
		{
			get
			{
				return (int)base.GetValue(Pivot.SelectedIndexProperty);
			}
			set
			{
				base.SetValue(Pivot.SelectedIndexProperty, value);
			}
		}
		public object SelectedItem
		{
			get
			{
				return base.GetValue(Pivot.SelectedItemProperty);
			}
			set
			{
				base.SetValue(Pivot.SelectedItemProperty, value);
			}
		}
		public object Title
		{
			get
			{
				return base.GetValue(Pivot.TitleProperty);
			}
			set
			{
				base.SetValue(Pivot.TitleProperty, value);
			}
		}
		public DataTemplate TitleTemplate
		{
			get
			{
				return base.GetValue(Pivot.TitleTemplateProperty) as DataTemplate;
			}
			set
			{
				base.SetValue(Pivot.TitleTemplateProperty, value);
			}
		}
		private bool EnoughItemsForManipulation
		{
			get
			{
				return base.get_Items().get_Count() > 1;
			}
		}
		private static void OnSelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			Pivot pivot = d as Pivot;
			if (pivot._ignorePropertyChange)
			{
				pivot._ignorePropertyChange = false;
				return;
			}
			pivot.UpdateSelectedIndex((int)e.get_OldValue(), (int)e.get_NewValue());
		}
		private static void OnSelectedItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			Pivot pivot = d as Pivot;
			if (pivot._ignorePropertyChange)
			{
				pivot._ignorePropertyChange = false;
				return;
			}
			pivot.UpdateSelectedItem(e.get_OldValue(), e.get_NewValue());
		}
		public Pivot()
		{
			PerfLog.BeginLogMarker(PerfMarkerEvents.MPC_PIVOT_STARTUP, PerfLog.Pivot);
			base.set_DefaultStyleKey(typeof(Pivot));
			base.add_SizeChanged(new SizeChangedEventHandler(this.OnSizeChanged));
			GestureHelper gestureHelper = GestureHelper.Create(this);
			gestureHelper.GestureStart += new EventHandler<GestureEventArgs>(this.OnGestureStart);
			gestureHelper.HorizontalDrag += new EventHandler<DragEventArgs>(this.OnHorizontalDrag);
			gestureHelper.Flick += new EventHandler<FlickEventArgs>(this.OnFlick);
			gestureHelper.GestureEnd += new EventHandler<EventArgs>(this.OnGesturesComplete);
			this._isDesignTime = DesignerProperties.get_IsInDesignTool();
			this._queuedIndexChanges = new Queue<int>(5);
			base.add_Loaded(new RoutedEventHandler(this.Pivot_Loaded));
		}
		private void Pivot_Loaded(object sender, RoutedEventArgs e)
		{
			base.remove_Loaded(new RoutedEventHandler(this.Pivot_Loaded));
			PerfLog.EndLogMarker(PerfMarkerEvents.MPC_PIVOT_STARTUP, PerfLog.Pivot);
		}
		public override void OnApplyTemplate()
		{
			PerfLog.BeginLogMarker(PerfMarkerEvents.MPC_PIVOT_APPLYTEMPLATE, PerfLog.Pivot);
			if (this._headers != null)
			{
				this._headers.SelectedIndexChanged -= new EventHandler<SelectedIndexChangedEventArgs>(this.OnHeaderSelectionChanged);
			}
			base.OnApplyTemplate();
			this._itemsPresenter = (base.GetTemplateChild("PivotItemPresenter") as ItemsPresenter);
			this._headers = (base.GetTemplateChild("HeadersListElement") as PivotHeadersControl);
			if (this._headers != null)
			{
				this._headers.SelectedIndexChanged += new EventHandler<SelectedIndexChangedEventArgs>(this.OnHeaderSelectionChanged);
				this.UpdateHeaders();
			}
			if (base.get_Items().get_Count() > 0)
			{
				if (this.SelectedIndex < 0)
				{
					this.SelectedIndex = 0;
				}
				else
				{
					this.UpdateSelectedIndex(-1, this.SelectedIndex);
				}
			}
			this.UpdateVisibleContent(this.SelectedIndex);
			this.SetSelectedHeaderIndex(this.SelectedIndex);
			this._templateApplied = true;
			PerfLog.EndLogMarker(PerfMarkerEvents.MPC_PIVOT_APPLYTEMPLATE, PerfLog.Pivot);
		}
		protected override Size ArrangeOverride(Size finalSize)
		{
			PerfLog.BeginLogMarker(PerfMarkerEvents.MPC_PIVOT_ARRANGE, PerfLog.Pivot);
			Size result = base.ArrangeOverride(finalSize);
			PerfLog.EndLogMarker(PerfMarkerEvents.MPC_PIVOT_ARRANGE, PerfLog.Pivot);
			return result;
		}
		protected override Size MeasureOverride(Size availableSize)
		{
			PerfLog.BeginLogMarker(PerfMarkerEvents.MPC_PIVOT_MEASURE, PerfLog.Pivot);
			Size result = base.MeasureOverride(availableSize);
			PerfLog.EndLogMarker(PerfMarkerEvents.MPC_PIVOT_MEASURE, PerfLog.Pivot);
			return result;
		}
		protected virtual void OnLoadingPivotItem(PivotItem item)
		{
			if (item != null && item.get_Visibility() == 1)
			{
				item.set_Visibility(0);
			}
			SafeRaise.Raise<PivotItemEventArgs>(this.LoadingPivotItem, this, () => new PivotItemEventArgs(item));
		}
		protected virtual void OnLoadedPivotItem(PivotItem item)
		{
			SafeRaise.Raise<PivotItemEventArgs>(this.LoadedPivotItem, this, () => new PivotItemEventArgs(item));
			this.OptimizeVisuals();
		}
		private void OptimizeVisuals()
		{
			int selectedIndex = this.SelectedIndex;
			if (selectedIndex >= 0 && base.get_Items().get_Count() > 1)
			{
				PivotItem next = base.GetContainer(base.get_Items().get_Item(this.RollingIncrement(selectedIndex)));
				PivotItem previous = base.GetContainer(base.get_Items().get_Item(this.RollingDecrement(selectedIndex)));
				bool flag = true;
				if (next != null && previous != null && next.get_Visibility() == previous.get_Visibility() && previous.get_Visibility() == null)
				{
					flag = false;
				}
				if (flag)
				{
					base.get_Dispatcher().BeginInvoke(delegate
					{
						if (next != null && next.get_Visibility() == 1)
						{
							next.set_Visibility(0);
						}
						if (previous != next)
						{
							this.get_Dispatcher().BeginInvoke(delegate
							{
								if (previous != null && previous.get_Visibility() == 1)
								{
									previous.set_Visibility(0);
								}
							});
						}
					});
				}
			}
		}
		protected virtual void OnUnloadingPivotItem(PivotItemEventArgs e)
		{
			EventHandler<PivotItemEventArgs> unloadingPivotItem = this.UnloadingPivotItem;
			if (unloadingPivotItem != null)
			{
				unloadingPivotItem.Invoke(this, e);
			}
		}
		protected virtual void OnUnloadedPivotItem(PivotItemEventArgs e)
		{
			EventHandler<PivotItemEventArgs> unloadedPivotItem = this.UnloadedPivotItem;
			if (unloadedPivotItem != null)
			{
				unloadedPivotItem.Invoke(this, e);
			}
		}
		protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
		{
			int selectedIndex = this.SelectedIndex;
			int? num = default(int?);
			int count = base.get_Items().get_Count();
			if (e != null)
			{
				switch (e.get_Action())
				{
				case 0:
					if (this._templateApplied && e.get_NewStartingIndex() == selectedIndex)
					{
						num = new int?(selectedIndex);
					}
					break;
				case 1:
					num = new int?(selectedIndex);
					if (selectedIndex == e.get_OldStartingIndex() || selectedIndex >= count)
					{
						num = new int?(0);
					}
					break;
				}
			}
			if (num.get_HasValue())
			{
				int? num2 = num;
				int num3 = selectedIndex;
				this._animationHint = new AnimationDirection?((num2.GetValueOrDefault() < num3 && num2.get_HasValue()) ? AnimationDirection.Right : AnimationDirection.Left);
				this.SetSelectedIndexInternal(num.get_Value());
			}
			this.UpdateHeaders();
			this.OptimizeVisuals();
			base.OnItemsChanged(e);
			base.UpdateLayout();
		}
		protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
		{
			base.PrepareContainerForItemOverride(element, item);
			PivotItem pivotItem = element as PivotItem;
			int selectedIndex = this.SelectedIndex;
			if (selectedIndex >= 0 && base.get_Items().get_Count() > selectedIndex)
			{
				object obj = base.get_Items().get_Item(selectedIndex);
				if (item == obj)
				{
					if (pivotItem != null && this._skippedLoadingPivotItem)
					{
						this.OnLoadingPivotItem(pivotItem);
						if (this._skippedSwapVisibleContent)
						{
							this.OnLoadedPivotItem(pivotItem);
						}
					}
					return;
				}
			}
			if (pivotItem != null)
			{
				this.UpdateItemVisibility(pivotItem, false);
				if (pivotItem.get_Visibility() == null)
				{
					pivotItem.set_Visibility(1);
				}
			}
		}
		private void UpdateSelectedIndex(int oldIndex, int newIndex)
		{
			object selectedItem = null;
			int count = base.get_Items().get_Count();
			if (newIndex >= 0 && newIndex < count)
			{
				selectedItem = base.get_Items().get_Item(newIndex);
			}
			else
			{
				if (count > 0 && !this._isDesignTime)
				{
					this._ignorePropertyChange = true;
					this.SelectedIndex = oldIndex;
					throw new ArgumentException("SelectedIndex");
				}
			}
			if (newIndex < 0 && base.get_Items().get_Count() > 0 && !this._isDesignTime)
			{
				this._ignorePropertyChange = true;
				this.SelectedIndex = 0;
				throw new ArgumentException("SelectedIndex");
			}
			this.SelectedItem = selectedItem;
		}
		private void SetSelectedIndexInternal(int newIndex)
		{
			this._ignorePropertyChange = true;
			this.SelectedIndex = newIndex - 1;
			this.SelectedIndex = newIndex;
		}
		private void UpdateSelectedItem(object oldValue, object newValue)
		{
			if (newValue == null && base.get_Items().get_Count() > 0 && !this._isDesignTime)
			{
				this._ignorePropertyChange = true;
				this.SelectedItem = oldValue;
				throw new ArgumentException("SelectedItem");
			}
			int num = base.get_Items().IndexOf(oldValue);
			int num2 = base.get_Items().IndexOf(newValue);
			if (!this._animationHint.get_HasValue() && num != -1 && num2 != -1)
			{
				this._animationHint = new AnimationDirection?((this.RollingIncrement(num2) == num) ? AnimationDirection.Right : AnimationDirection.Left);
			}
			PivotItem container = base.GetContainer(newValue);
			PivotItem container2 = base.GetContainer(oldValue);
			this.BeginAnimateContent(num2, container2, this._animationHint.GetValueOrDefault());
			this.SetSelectedHeaderIndex(num2);
			if (this.SelectedIndex != num2)
			{
				this.SetSelectedIndexInternal(num2);
			}
			if (oldValue != null)
			{
				this.OnUnloadingPivotItem(new PivotItemEventArgs(container2));
			}
			if (container != null)
			{
				this.OnLoadingPivotItem(container);
			}
			else
			{
				this._skippedLoadingPivotItem = true;
			}
			List<object> list = new List<object>();
			list.Add(oldValue);
			IList arg_F9_0 = list;
			List<object> list2 = new List<object>();
			list2.Add(newValue);
			this.OnSelectionChanged(new SelectionChangedEventArgs(arg_F9_0, list2));
		}
		private void SetSelectedHeaderIndex(int selectedIndex)
		{
			try
			{
				this._updatingHeaderItems = true;
				if (this._headers != null && base.get_Items().get_Count() > 0)
				{
					this._headers.SelectedIndex = selectedIndex;
				}
			}
			finally
			{
				this._updatingHeaderItems = false;
			}
		}
		private int RollingIncrement(int index)
		{
			index++;
			if (index >= base.get_Items().get_Count())
			{
				return 0;
			}
			return index;
		}
		private int RollingDecrement(int index)
		{
			index--;
			if (index >= 0)
			{
				return index;
			}
			return base.get_Items().get_Count() - 1;
		}
		protected virtual void OnSelectionChanged(SelectionChangedEventArgs e)
		{
			SelectionChangedEventHandler selectionChanged = this.SelectionChanged;
			if (selectionChanged != null)
			{
				selectionChanged.Invoke(this, e);
			}
		}
		private void OnGestureStart(object sender, GestureEventArgs e)
		{
			this._isHorizontalDragging = false;
			if (this._clickedHeadersControl != null)
			{
				this._clickedHeadersControl._wasClicked = false;
				this._clickedHeadersControl._cancelClick = false;
			}
		}
		private void OnGesturesComplete(object sender, EventArgs e)
		{
			if (this.EnoughItemsForManipulation)
			{
				DragEventArgs dragEventArgs = e as DragEventArgs;
				if (dragEventArgs != null && this._isHorizontalDragging)
				{
					double x = dragEventArgs.CumulativeDistance.get_X();
					double num = Math.Abs(x);
					if (x != 0.0 && num >= this._actualWidth / 3.0)
					{
						this.NavigateByIndexChange((x <= 0.0) ? 1 : -1);
					}
				}
				if (!this._animating && this._headers != null && this._itemsPresenter != null)
				{
					TransformAnimator.EnsureAnimator(this._itemsPresenter, ref this._panAnimator);
					this._panAnimator.GoTo(this.CalculateContentDestination(AnimationDirection.Center), Pivot.PivotAnimationDuration, this.QuarticEase);
					this._headers.RestoreHeaderPosition(Pivot.PivotAnimationDuration);
				}
			}
			this._isHorizontalDragging = false;
		}
		private void OnFlick(object sender, FlickEventArgs e)
		{
			if (this._clickedHeadersControl != null)
			{
				this._clickedHeadersControl._wasClicked = false;
				this._clickedHeadersControl._cancelClick = true;
			}
			if (this.EnoughItemsForManipulation)
			{
				int num = (int)e.Angle;
				if (num == 0 || num == 180)
				{
					this.NavigateByIndexChange((num == 180) ? 1 : -1);
				}
			}
		}
		private void OnHorizontalDrag(object sender, DragEventArgs e)
		{
			this._isHorizontalDragging = true;
			if (this._clickedHeadersControl != null)
			{
				this._clickedHeadersControl._cancelClick = true;
			}
			if (!this._animating && this.EnoughItemsForManipulation && this._itemsPresenter != null)
			{
				TransformAnimator.EnsureAnimator(this._itemsPresenter, ref this._panAnimator);
				double num = Math.Abs(e.DeltaDistance.get_X());
				if (!e.IsTouchComplete && !this._animating && this._panAnimator != null && this._headers != null)
				{
					TimeSpan timeSpan = TimeSpan.FromSeconds(num / 600.0);
					this._panAnimator.GoTo(e.CumulativeDistance.get_X(), new Duration(timeSpan));
					this._headers.PanHeader(e.CumulativeDistance.get_X(), this._actualWidth, new Duration(timeSpan));
				}
			}
		}
		private PivotHeaderItem CreateHeaderBindingControl(object item)
		{
			PivotHeaderItem pivotHeaderItem = new PivotHeaderItem();
			pivotHeaderItem.set_ContentTemplate(this.HeaderTemplate);
			PivotHeaderItem pivotHeaderItem2 = pivotHeaderItem;
			Binding binding = new Binding();
			binding.set_Source(item);
			Binding binding2 = binding;
			if (item is PivotItem)
			{
				binding2.set_Path(new PropertyPath("Header", new object[0]));
			}
			PivotHeaderItem result;
			try
			{
				binding2.set_Mode(1);
				pivotHeaderItem2.SetBinding(ContentControl.ContentProperty, binding2);
				result = pivotHeaderItem2;
			}
			catch
			{
				if (!this._isDesignTime)
				{
					throw;
				}
				result = null;
			}
			return result;
		}
		private void UpdateHeaders()
		{
			if (this._headers != null)
			{
				List<PivotHeaderItem> list = new List<PivotHeaderItem>();
				int count = base.get_Items().get_Count();
				for (int i = 0; i < count; i++)
				{
					object item = base.get_Items().get_Item(i);
					list.Add(this.CreateHeaderBindingControl(item));
				}
				try
				{
					this._updatingHeaderItems = true;
					this._headers.set_ItemsSource((count == 0) ? null : list);
				}
				finally
				{
					this._updatingHeaderItems = false;
				}
			}
		}
		private void OnHeaderSelectionChanged(object sender, SelectedIndexChangedEventArgs e)
		{
			if (!this._updatingHeaderItems)
			{
				this._animationHint = new AnimationDirection?(AnimationDirection.Left);
				this.SelectedIndex = e.SelectedIndex;
			}
		}
		private void NavigateByIndexChange(int indexDelta)
		{
			if (this._animating && this._queuedIndexChanges != null)
			{
				this._queuedIndexChanges.Enqueue(indexDelta);
				return;
			}
			int num = this.SelectedIndex;
			if (num != -1)
			{
				this._animationHint = new AnimationDirection?((indexDelta > 0) ? AnimationDirection.Left : AnimationDirection.Right);
				num += indexDelta;
				if (num >= base.get_Items().get_Count())
				{
					num = 0;
				}
				else
				{
					if (num < 0)
					{
						num = base.get_Items().get_Count() - 1;
					}
				}
				if (this._clickedHeadersControl != null)
				{
					this._clickedHeadersControl._wasClicked = false;
					this._clickedHeadersControl._cancelClick = true;
				}
				this.SelectedIndex = num;
			}
		}
		private int GetPreviousIndex()
		{
			int count = base.get_Items().get_Count();
			if (count > 0)
			{
				int num = this.SelectedIndex - 1;
				if (num < 0)
				{
					num = count - 1;
				}
				return num;
			}
			return 0;
		}
		private int GetNextIndex()
		{
			int count = base.get_Items().get_Count();
			if (count > 0)
			{
				int num = this.SelectedIndex + 1;
				if (num > count)
				{
					num = 0;
				}
				return num;
			}
			return 0;
		}
		private void UpdateVisibleContent(int index)
		{
			if (this.TryHasItemsHost())
			{
				for (int i = 0; i < this._itemsPanel.get_Children().get_Count(); i++)
				{
					UIElement element = this._itemsPanel.get_Children().get_Item(i);
					this.UpdateItemVisibility(element, i == index);
				}
			}
		}
		private bool TryHasItemsHost()
		{
			if (this._itemsPanel != null)
			{
				return true;
			}
			if (base.get_ItemContainerGenerator() != null)
			{
				DependencyObject dependencyObject = base.get_ItemContainerGenerator().ContainerFromIndex(0);
				if (dependencyObject != null)
				{
					this._itemsPanel = (VisualTreeHelper.GetParent(dependencyObject) as Panel);
					return this._itemsPanel != null;
				}
			}
			return false;
		}
		protected virtual void UpdateItemVisibility(UIElement element, bool toVisible)
		{
			if (element != null)
			{
				element.set_Opacity((double)(toVisible ? 1 : 0));
				element.set_IsHitTestVisible(toVisible);
				if (toVisible && element.get_Visibility() == 1)
				{
					element.set_Visibility(0);
				}
				if (this._isDesignTime)
				{
					TranslateTransform translateTransform = TransformAnimator.GetTranslateTransform(element);
					if (translateTransform != null)
					{
						translateTransform.set_X(toVisible ? 0.0 : (-base.get_ActualWidth()));
					}
				}
			}
		}
		private double CalculateContentDestination(AnimationDirection direction)
		{
			double result = 0.0;
			double actualWidth = base.get_ActualWidth();
			switch (direction)
			{
			case AnimationDirection.Left:
				result = -actualWidth;
				break;
			case AnimationDirection.Right:
				result = actualWidth;
				break;
			}
			return result;
		}
		private static AnimationDirection InvertAnimationDirection(AnimationDirection direction)
		{
			switch (direction)
			{
			case AnimationDirection.Left:
				return AnimationDirection.Right;
			case AnimationDirection.Right:
				return AnimationDirection.Left;
			default:
				return direction;
			}
		}
		private void BeginAnimateContent(int newIndex, PivotItem oldItem, AnimationDirection animationDirection)
		{
			if (this._isDesignTime)
			{
				this.SwapVisibleContent(oldItem, newIndex);
				return;
			}
			if (this._itemsPresenter != null)
			{
				this._animating = true;
				TransformAnimator.EnsureAnimator(this._itemsPresenter, ref this._panAnimator);
				PivotItem container = base.GetContainer(this.SelectedItem);
				if (container != null)
				{
					container.MoveTo(AnimationDirection.Center);
				}
				if (this._headers != null && animationDirection != AnimationDirection.Center)
				{
					this._headers.AnimationDirection = animationDirection;
				}
				this._panAnimator.GoTo(this.CalculateContentDestination(animationDirection), Pivot.PivotAnimationDuration, delegate
				{
					this._panAnimator.GoTo(this.CalculateContentDestination(Pivot.InvertAnimationDirection(animationDirection)), Pivot.ZeroDuration);
					this.SwapVisibleContent(oldItem, newIndex);
					PivotItem container2 = this.GetContainer(this.SelectedItem);
					if (container2 != null)
					{
						container2.MoveTo(animationDirection);
					}
					this._panAnimator.GoTo(0.0, Pivot.PivotAnimationDuration, this.QuarticEase, delegate
					{
						this._animationHint = default(AnimationDirection?);
						this._animating = false;
						this.ProcessQueuedChanges();
					});
				});
				return;
			}
			this._skippedSwapVisibleContent = true;
		}
		private void SwapVisibleContent(PivotItem oldItem, int newIndex)
		{
			if (oldItem != null)
			{
				this.OnUnloadedPivotItem(new PivotItemEventArgs(oldItem));
			}
			this.UpdateVisibleContent(newIndex);
			this.OnLoadedPivotItem(base.GetContainer(this.SelectedItem));
		}
		private void ProcessQueuedChanges()
		{
			if (this._queuedIndexChanges != null && this._queuedIndexChanges.get_Count() > 0 && !this._animating)
			{
				int indexDelta = this._queuedIndexChanges.Dequeue();
				this.NavigateByIndexChange(indexDelta);
			}
		}
		private void OnSizeChanged(object sender, SizeChangedEventArgs e)
		{
			this._actualWidth = base.get_ActualWidth();
			RectangleGeometry rectangleGeometry = new RectangleGeometry();
			rectangleGeometry.set_Rect(new Rect(0.0, 0.0, this._actualWidth, base.get_ActualHeight()));
			base.set_Clip(rectangleGeometry);
			if (this._isDesignTime)
			{
				this.UpdateVisibleContent(this.SelectedIndex);
			}
		}
	}
}
